package com.gm.census.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.gm.census.service.BaseService;
import com.gm.census.service.PagedListResult;
import com.google.appengine.api.datastore.QueryResultIterator;

public class GAEPagedListResult<E> implements PagedListResult<List<E>> {

	QueryResultIterator<E> it;
	String currentIndex;
	List<E> list;

	public GAEPagedListResult(QueryResultIterator<E> it) {
		super();
		this.it = it;
		this.currentIndex = it.getCursor().toWebSafeString();
	}

	@Override
	public String getNextPageIndex() {
		if (it.hasNext()) {
			getReturn().size();
		}
		return it.getCursor().toWebSafeString();
	}

	@Override
	public List<E> getReturn() {
		if (it.hasNext() || list == null) {
			list = new ArrayList<E>();
			while (it.hasNext()) {
				list.add(it.next());
			}
		}
		return list;
	}

	@Override
	public String getCurrentPageIndex() {
		return currentIndex;
	}

}
